package queryid

import (
	"errors"
	"gitee.com/hlwqds/clientSimulation/src/lib/myjson"
	"github.com/tidwall/gjson"
)

const (
	userQueryIDQueryCusID    = "0"
	userQueryIDInsertCusID   = "1"
	userQueryIDCommonTest    = "4"
	userQueryGroupCommonTest = "5"
	userQuerySeatCommonTest = "6"
	userQueryPlayVoice2CommonTest = "7"
	userQueryOutlineNumberCommonTest = "8"
	userQueryIVRFlowCommonTest = "9"
	userQueryEndCommonTest = "10"
)

type actionType int

const (
	actionTypePlayVoice = 1
	actionTypeToGroup = 3
	actionTypeToSeat = 4
	actionTypeToPlayVoice2 = 2
	actionTypeToOutlineNumber = 5
	actionTypeToIVRFlow = 6

	actionTypeFinish    = 7
)

type nextActionFinishNode struct {
	Action actionType `json:"action"`
}

type commonQueryFinshedRsp struct {
	Retcode    int                   `json:"retcode"`
	Reason     string                `json:"reason"`
	NextAction *nextActionFinishNode `json:"nextAction,omitempty"`
}
type commonQueryFinshedStru struct {
	Response *commonQueryFinshedRsp `json:"response,omitempty"`
}

type nextActionPlayVoiceParams struct {
	VoiceId         uint64 `json:"voiceId,omitempty"`
	VoiceName       string `json:"voiceName,omitempty"`
	VoiceTempId     uint64 `json:"voiceTempId,omitempty"`
	VoiceTempName   string `json:"voiceTempName,omitempty"`
	VoiceTempParams string `json:"voiceTempParams,omitempty"`
	AllowBreak      int    `json:"allowBreak,omitempty"`
}

type nextActionToGroupParams struct {
	AcdId          uint64 `json:"acdid,omitempty"`
	AcdName        string `json:"acdName,omitempty"`
	UseAcdValue    int    `json:"useAcdValue,omitempty"`
	QueueTime      int    `json:"queueTime,omitempty"`
	SwitchTimes    int    `json:"switchTimes"`
	RingTimeout    int    `json:"ringTimeout,omitempty"`
	CustomerMemory int    `json:"customerMemory,omitempty"`
}

type nextActionToSeatParams struct {
	WorkNumber        string `json:"workNumber,omitempty"`
	Number    string    `json:"number,omitempty"`
	QueueTime      int    `json:"queueTime,omitempty"`
	RingTimeout    int    `json:"ringTimeout,omitempty"`
}

type nextActionToVoice2Params struct {
	VoiceName        string `json:"voiceName,omitempty"`
	AllowBreak    int    `json:"allowBreak,omitempty"`
	GetKeyNumber      int    `json:"getKeyNumber,omitempty"`
	GetKeyTimeout    int    `json:"getKeyTimeout,omitempty"`
	EndWithHashKey    int    `json:"endWithHashKey,omitempty"`
}

type nextActionToOutlineNumber struct {
	Called        string `json:"called,omitempty"`
	OutNumber    string    `json:"outNumber,omitempty"`
}

type nextActionToIVRFlow struct {
	IvrFlowId        string `json:"ivrFlowId,omitempty"`
	IvrFlowName    string    `json:"ivrFlowName,omitempty"`
}

type nextActionPlayVoiceNode struct {
	Action actionType                 `json:"action"`
	Params *nextActionPlayVoiceParams `json:"params,omitempty"`
}

type nextActionPlayVoiceDigParams struct {
	VoiceId         uint64 `json:"voiceId,omitempty"`
	VoiceName       string `json:"voiceName,omitempty"`
	VoiceTempId     uint64 `json:"voiceTempId,omitempty"`
	VoiceTempName   string `json:"voiceTempName,omitempty"`
	VoiceTempParams string `json:"voiceTempParams,omitempty"`
	AllowBreak      int    `json:"allowBreak,omitempty"`
	GetKeyNumber    int    `json:"getKeyNumber,omitempty"`
	GetKeyTimeout   int    `json:"getKeyTimeout,omitempty"`
	EndWithHashKey  int    `json:"endWithHashKey,omitempty"`
}
type nextActionPlayVoiceDigNode struct {
	Action actionType                    `json:"action"`
	Params *nextActionPlayVoiceDigParams `json:"params,omitempty"`
}

type commonPlayVoiceDigRsp struct {
	Retcode     int                         `json:"retcode"`
	Reason      string                      `json:"reason"`
	UserQueryID string                      `json:"usrQueryId"`
	Variables   []variableMem               `json:"variables"`
	NextAction  *nextActionPlayVoiceDigNode `json:"nextAction,omitempty"`
}
type commonPlayVoiceDigStru struct {
	Response *commonPlayVoiceDigRsp `json:"response,omitempty"`
}
type commonActionNode struct {
	Action actionType  `json:"action"`
	Params interface{} `json:"params,omitempty"`
}
type commonIVRRsp struct {
	Retcode     int               `json:"retcode"`
	Reason      string            `json:"reason"`
	UserQueryID string            `json:"usrQueryId"`
	Variables   []variableMem     `json:"variables"`
	NextAction  *commonActionNode `json:"nextAction,omitempty"`
}
type commonIVRStru struct {
	Response *commonIVRRsp `json:"response,omitempty"`
}

type commonQueryCusIDRsp struct {
	Retcode     int                      `json:"retcode"`
	Reason      string                   `json:"reason"`
	UserQueryID string                   `json:"usrQueryId"`
	Variables   []variableMem            `json:"variables"`
	NextAction  *nextActionPlayVoiceNode `json:"nextAction,omitempty"`
}
type commonQueryCusIDStru struct {
	Response *commonQueryCusIDRsp `json:"response,omitempty"`
}

type keyBackRsp struct {
	Retcode     int                      `json:"retcode"`
	Reason      string                   `json:"reason"`
	UserQueryID string                   `json:"usrQueryId"`
	Variables   []variableMem            `json:"variables"`
	NextAction  *nextActionPlayVoiceNode `json:"nextAction,omitempty"`
	VirtualKey  string                   `json:"virtualKey"`
}
type keyBackStru struct {
	Response *keyBackRsp `json:"response,omitempty"`
}

func (p *ReqPriD) hanleCommonIVRQueryCusID(req []byte) (rsp []byte, err error) {
	tel := gjson.GetBytes(req, myjson.GenPathForGJSON(jsonKeyRequest, jsonKeyCaller))

	code, err := p.getIdentiCodeByTelNumber(tel.String())
	p.logf(LOG_INFO, "code:%v, err:%v", code, err)

	if err != nil {
		return rsp, err
	}
	if code == "" {
		//如果没有找到，我们认为这次查询是失败的，构建失败消息
		rsp, err = p.GenNotFindResp()
		return rsp, err
	}

	rsp, err = p.GenReturnCodeForVoicePlay(code)
	return rsp, err
}

func (p *ReqPriD) hanleCommonIVRInsertCusID(req []byte) (rsp []byte, err error) {
	tel := gjson.GetBytes(req, myjson.GenPathForGJSON(jsonKeyRequest, jsonKeyCaller)).String()
	code := gjson.GetBytes(req, myjson.GenPathForGJSON(jsonKeyRequest, jsonKeyInputKeys)).String()

	err = p.insertIdentiCodeByTelNumber(tel, code)
	p.logf(LOG_INFO, "code:%v, err:%v", code, err)

	if err != nil {
		return rsp, err
	}

	rsp, err = p.GenReturnCodeForVoicePlayTest1(code)
	return rsp, err
}

func (p *ReqPriD) handleCommonIVRQuery(req []byte) (rsp []byte, err error) {
	usrQueryID := gjson.GetBytes(req, myjson.GenPathForGJSON(jsonKeyRequest, jsonKeyUserQueryID))
	p.logf(LOG_INFO, "usrQueryID:%s", usrQueryID.String())
	switch usrQueryID.String() {
	case userQueryIDQueryCusID:
		rsp, err = p.hanleCommonIVRQueryCusID(req)
		break
	case userQueryIDInsertCusID:
		rsp, err = p.hanleCommonIVRInsertCusID(req)
	case userQueryIDCommonTest:
		rsp, err = p.GenReturnCodeForAction1()
	case userQueryGroupCommonTest:
		rsp, err = p.GenReturnForGroup()
	case userQuerySeatCommonTest:
		rsp, err = p.GenReturnForSeat()
	case userQueryPlayVoice2CommonTest:
		rsp, err = p.GenReturnForPlayVoice2()
	case userQueryOutlineNumberCommonTest:
		rsp, err = p.GenReturnForOutlineNumber()
	case userQueryIVRFlowCommonTest:
		rsp, err = p.GenReturnForIVRFlow()
	case userQueryEndCommonTest:
		rsp, err = p.GenReturnForEnd()
	default:
		err = errors.New("unknow usrQueryID")
		goto _exit
	}

_exit:
	return rsp, err
}
func (p *ReqPriD) handleKeyBack() (rsp []byte, err error) {
	rsp, err = p.GenKeyBackTest1()
	return rsp, err
}
